package com.et.sale.ticket.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.et.sale.ticket.entity.TicketEntity;
import com.et.sale.ticket.service.TicketService;


@Controller
@RequestMapping("/ticket")
public class TicketController {
	
	
	@Autowired TicketService service;
	
	
	@RequestMapping("/add")
	public String addSave(TicketEntity entity){
		
		//1：入参的检查
		
		//2: 调用service 插入到数据库
		service.addSave(entity);
		
	    //3: 返回到列表页面 
		return "forward:/ticket/list.do";
	}
	
	@RequestMapping("/delete")
	public String delete(int  id){
		
		//1：入参的检查
		
		//2: 调用service 插入到数据库
		service.delete(id);
		
		//3: 返回到列表页面 
		return "forward:/ticket/list.do";
	}
	
	@RequestMapping("/update")
	public String updateSave(TicketEntity entity){
		
		//1：入参的检查
		
		//2: 调用service 插入到数据库
		service.updateSave(entity);
		
		//3: 返回到列表页面 
		return "forward:/ticket/list.do";
	}
	
	
	/**
	 * http://localhost:8888/springmvcmavenproject/ticket/list.do?currentPage=1&pageSize=2
	 * @param currentPage  当前页
	 * @param pageSize     一页多少条
	 * @return
	 */
	
	@RequestMapping("/list")
	public ModelAndView list( Integer currentPage,  Integer pageSize){
		
		if(null == currentPage ){
			currentPage = 1;
		}
		
		if(null ==  pageSize){
			pageSize = 2;
		}
		
		ModelAndView view = new ModelAndView();
		currentPage = pageSize*(currentPage -1);
		List<TicketEntity> list  = service.list(currentPage,pageSize);
		System.out.println(list);
		
		view.addObject("tickets", list);
		
		
		
		view.setViewName("ticket/list");
		//3: 返回到列表页面 
		return view;
	}
	@RequestMapping("/findById")
	public ModelAndView findById( Integer id){
		
		ModelAndView view = new ModelAndView();
		TicketEntity entity  = service.findById(id);
		view.addObject("entity", entity);
		view.setViewName("ticket/pre_edit");
		//3: 返回到列表页面 
		return view;
	}
	
	
	

}
